package yl.hs.bmipfull.security;

import org.springframework.web.servlet.HandlerInterceptor;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.utils.ResultCode;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.WebResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 张凯
 * @apiNote 主机程序请求拦截器
 * @since 2021/11/17 10:33 上午 创建
 */
public class HostHandler implements HandlerInterceptor {
    
    IProductionLineService lineService;

    public HostHandler(){

    }

    public HostHandler(IProductionLineService lineService){
        this.lineService = lineService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String hostToken = request.getHeader("hosttoken");
        if (!Tl.isEmpty(hostToken)) {
            try {
                String decryptHostToken = Aes.decrypt64(hostToken);
                String[] splitToken = decryptHostToken.split("\\@");
                if (splitToken.length == 3) {
                    if ((!Tl.isEmpty(splitToken[2])) && "HOST".equals(splitToken[2])) {
                        if (!Tl.isEmpty(splitToken[1])){
                            ProductionLineEntity lineEntity = lineService.findByName(splitToken[1]);
                            if (lineEntity != null) {
                                if(request.getSession().getAttribute("line")==null) {
                                    request.getSession().setAttribute("line", lineEntity);
                                }
                                return true;
                            } else {
                                writeResponse(new WebResult(ResultCode.UNAUTHOR,"生产线不存在!"),response);
                                return false;
                            }
                        }else {
                            writeResponse(new WebResult(ResultCode.UNAUTHOR.code),response);
                            return false;
                        }
                    }
                }
                writeResponse(new WebResult(ResultCode.UNAUTHOR.code),response);
                return false;

            } catch (Exception e) {
                e.printStackTrace();
                writeResponse(new WebResult(ResultCode.UNAUTHOR.code),response);
                return false;
            }
        } else {
            writeResponse(new WebResult(ResultCode.UNAUTHOR.code),response);
            return false;
        }
    }

    public void writeResponse(WebResult result, HttpServletResponse response) {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(Tl.toJson(result));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}
